﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace GotLyrics
{
    public abstract class LyricGrabber : ILyricGrabber
    {
        #region ILyricGrabber Members

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        public virtual string Name
        {
            get { return GetType().Name; }
        }

        private bool _enabled = true;
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="ILyricGrabber"/> is enabled.
        /// </summary>
        /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        /// <summary>
        /// The website of this lyrics provider. This is pointing to the main page on the website.
        /// </summary>
        public abstract string WebSiteUrl { get; }

        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public string Execute(LyricGrabberContext context)
        {
            if (!Enabled) return null;

            return GrabLyric(context);
        }

        #endregion

        /// <summary>
        /// Grabs the lyric based on the current <see cref="Context"/> from the webservice.
        /// </summary>
        /// <returns>The lyric if one was found, otherwise <c>null</c>.</returns>
        protected abstract string GrabLyric(LyricGrabberContext context);

        private WebClient _wc;
        /// <summary>
        /// Gets the web client.
        /// </summary>
        /// <value>The web client.</value>
        public virtual WebClient WebClient
        {
            get
            {
                if (_wc == null)
                {
                    _wc = new WebClient();
                    _wc.Encoding = Encoding.UTF8;
                }

                return _wc;
            }
        }
    }
}
